package leetcode.offer;

/**
 * ClassName: NumberIn2DArray
 * Description: leetcode-剑指Offer 04 二维数组中的查找
 * date: 2021-11-01 17:21
 *
 * @author liyifan
 */
public class NumberIn2DArray {
    public boolean findNumberIn2DArray(int[][] matrix, int target){
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return false;
        return findRecur(matrix, target,0, matrix[0].length - 1, 0, matrix.length - 1);
    }

    public boolean findRecur(int[][] matrix, int target, int left, int right, int up, int down){
        if (left <= right && up <= down) {
            int temp = matrix[up][right];
            if (temp == target) {
                return true;
            } else if (temp > target) {
                return findRecur(matrix, target, left, right - 1, up, down);
            } else {
                return findRecur(matrix,target,left,right,up + 1, down);
            }
        } else {
            return false;
        }
    }
}
